
import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author NamPhong
 */
public class DES_Stype {
    public static void main(String[] args) {
        String strFirst = "Nam Phong";
        System.out.println("Chuoi ban dau: "+strFirst);
        try {
            // Xac dinh thuat toan ma hoa
            Cipher ci = Cipher.getInstance("BlowFish");
            // Tao key cho thuat toan
            KeyGenerator kgt = KeyGenerator.getInstance("BlowFish");
            SecretKey sck = kgt.generateKey();
            // ma hoa
            ci.init(Cipher.ENCRYPT_MODE, sck);
            // chuyen chuoi ma hoa tu dang so sang text
            byte[] strByteEn = ci.doFinal(strFirst.getBytes());
            String strEn = Base64.encode(strByteEn);
            System.out.println("Chuoi ma hoa: "+strEn);
            // giai ma
            ci.init(Cipher.DECRYPT_MODE, sck);
            // chuyen chuoi giai ma;
            byte[] strByteDe = ci.doFinal(Base64.decode(strEn));
            String strDe = new String(strByteDe);
            System.out.println("Chuoi giai ma: "+strDe);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(DES_Stype.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchPaddingException ex) {
            Logger.getLogger(DES_Stype.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeyException ex) {
            Logger.getLogger(DES_Stype.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalBlockSizeException ex) {
            Logger.getLogger(DES_Stype.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadPaddingException ex) {
            Logger.getLogger(DES_Stype.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Base64DecodingException ex) {
            Logger.getLogger(DES_Stype.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
